import {
  dealFlowData
} from "./decrease.js"

//计算涨跌幅，每日涨跌幅
//就是特定的
function dealDayList(data1, num) {
  if (data1.array.length <= num) {
    return [{
      name: '最后',
      num: 0,
      time: data1.time[num],
      this_num: data1.num[0]
    }, {
      name: '最后',
      num: 0,
      time: data1.time[num],
      this_num: data1.num[0]
    }, {
      name: '最后',
      num: 0,
      time: data1.time[num],
      this_num: data1.num[0]
    }]
  }
  //最大的涨值，涨跌值，
  let end_array = data1.array.slice(-num);
  let this_time = data1.time.slice(-num);
  let max_num = Math.max(...end_array);
  let min_num = Math.min(...end_array);

  let max_time = end_array.findIndex((item) => {
    return item === max_num;
  });
  let min_time = end_array.findIndex((item) => {
    return item === min_num;
  });

  //处理数值的返回值的位置，
  let this_num_array = parseFloat(data1.num[data1.num.length - 1]);
  let deal_flow_data=dealFlowData({
    num: data1.num,
    inflow: data1.inflow,
    time: data1.time,
   }, num,2);
  let info_data = [{
    name: num + "天内最大涨跌值",
    num: max_num.toFixed(2),
    time: this_time[max_time],
    this_num: (this_num_array * (100 + parseFloat(max_num.toFixed(2))) / 100).toFixed(2),
  },
  {
    name: num + "天内最大跌幅值",
    num: min_num.toFixed(2),
    time: this_time[min_time],
    this_num: (this_num_array * (100 + parseFloat(min_num.toFixed(2))) / 100).toFixed(2),
  },
  //日期有问题
  {
    name: num + "天内持续涨幅",
    num: parseFloat(deal_flow_data[0].num).toFixed(2),
    time: deal_flow_data[0].min_time,
    this_num: this_num_array * (100 + parseFloat(deal_flow_data[0].num)) / 100,
  },
  {
    name: num + "天内持续跌幅",
    num: parseFloat(deal_flow_data[1].num).toFixed(2),
    time: deal_flow_data[0].min_time,
    this_num: this_num_array * (100 + parseFloat(deal_flow_data[1].num)) / 100,
  },
  ];
  return info_data;
}

//计算涨跌幅数值

export const initData = (this_data, day_list, this_time, inflow) => {
  let end_araay = [];
  let end_araay40 = [];
  //折线图的数据处理
  let array_10_num = [];
  let array_40_num = [];
  let array_10_num_f = [];
  let array_10_num_z = [];
  let array_40_num_f = [];
  let array_40_num_z = [];
  let array_20_num_z = [];
  let array_20_num_f = [];
  //涨跌幅的数值
  let array_10_data_z = [];
  let array_10_data_f = [];
  let array_20_data_z = [];
  let array_20_data_f = [];
  let array_40_data_z = [];
  let array_40_data_f = [];
  //折线图的股票的数值
  let array_10_this_zhang = [];

  
  let num_length = day_list.array.length;
  this_data.forEach((Element, index) => {
    let all_data = {
      array: day_list.array.slice(0, num_length - index),
      time: day_list.time.slice(0, num_length - index),
      inflow: day_list.inflow.slice(0, num_length - index),
      num: day_list.num.slice(0, num_length - index),
    }
    //现在处理文件
    let item = dealDayList(all_data, 10);
    let new_item20 = dealDayList(all_data, 20);
    // let item40 = dealFlowData(all_data, 40);
    let item40 = dealDayList(all_data, 40);
    end_araay40.push(item40)
    end_araay.push(item)
    array_10_num.push(item[0].num);
    let item40_2 = item40 != null ? item40[1].num : 0;
    array_40_num.push(item40_2);

    array_10_this_zhang.push(parseFloat(item[0].this_num));

    let this_10_z = item != null ? item[2].num : 0;
    let this_10_f = item[3] ? item[3].num : 0;
    array_10_num_z.push(this_10_z)
    array_10_num_f.push(this_10_f)

    let this_40_z = item40 != null ? item40[2].num : 0;
    let this_40_f = item40[3] ? item40[3].num : 0;
    array_40_num_z.push(this_40_z)
    array_40_num_f.push(this_40_f);
    let this_20_z = new_item20 != null ? new_item20[2].num : 0;
    let this_20_f = new_item20[3] ? new_item20[3].num : 0;
    array_20_num_z.push(this_20_z)
    array_20_num_f.push(this_20_f)

    let deal_num_item=day_list.inflow[day_list.inflow.length - index - 1];
    array_10_data_z.push(
      deal_num_item+parseFloat(this_10_z)
    )
    array_10_data_f.push(deal_num_item+parseFloat(this_10_f)
    )
    array_20_data_z.push( deal_num_item+parseFloat(this_20_z)
    )
    array_20_data_f.push(deal_num_item+parseFloat(this_20_f)
    )
    array_40_data_z.push(deal_num_item+parseFloat(this_40_z)
    )
    array_40_data_f.push(deal_num_item+parseFloat(this_40_f)
    )

  })
  return [{
    num: array_10_num.reverse(),
    name: '10天最大涨幅'
  }, {
    num: array_10_this_zhang,
    name: '10天最大涨幅数值'
  },
  {
    num: end_araay,
    name: '10天最大涨幅详情'
  },
  {
    num: array_40_num,
    name: '40天最大涨幅'
  },
  {
    num: end_araay40,
    name: '40天最大涨幅详情'
  },
  //涨跌幅列表
  {
    num: array_10_num_z.reverse(),
    name: '10天内持续涨幅'
  },
  {
    num: array_10_num_f.reverse(),
    name: '10天内持续跌幅'
  }, {
    num: array_40_num_z.reverse(),
    name: '40天内持续涨幅'
  },
  {
    num: array_40_num_f.reverse(),
    name: '40天内持续跌幅'
  }, {
    num: array_20_num_z.reverse(),
    name: '20天内持续涨幅'
  }, {
    num: array_20_num_f.reverse(),
    name: '20天内持续跌幅'
  },
  //涨跌幅的真实数值
  {
    num: array_10_data_z.reverse(),
    name: '10天内持续数值涨幅'
  }, {
    num: array_10_data_f.reverse(),
    name: '10天内持续数值跌幅'
  }, {
    num: array_20_data_z.reverse(),
    name: '20天内持续数值涨幅'
  }, {
    num: array_20_data_f.reverse(),
    name: '20天内持续数值跌幅'
  }, {
    num: array_40_data_z.reverse(),
    name: '40天内持续数值涨幅'
  }, {
    num: array_40_data_f.reverse(),
    name: '40天内持续数值跌幅'
  }

  ]


}
//生成line和area
export const dealLineAreaMap=(data_num,warnLine)=>{
  let line_data=null;
  let area_data=null;
  let map_data=null;
 
  let array3_num = data_num;

  var maxVal = Math.max.apply(null, array3_num);
  var minVal = parseInt(Math.min.apply(null, array3_num));
  var line_data_array =[];
  let area_data_item=[];
  let map_data_array=[];
  let color_array=['#409eff','#e6a23c','#ed4014']
  let color_array2=['#409eff','#e6a23c','#ed4014']
  warnLine.forEach((item,index)=>{
      const item_warnProp=1 - (maxVal - item) / (maxVal - minVal);
      area_data_item.push({
        offset: item_warnProp,
        color: color_array2[index], // 标线处的颜色
      })
      area_data_item.push({
        offset: item_warnProp,
        color: color_array2[index+1], // 标线处的颜色
      })
      line_data_array.push({
        silent: false,
        name: "涨跌均线" + warnLine[index],
        yAxis: warnLine[index],
        lineStyle: {
          width: 1,
          //type: "solid",
          color: "#e72243",
        },
        label: {
          // padding: [-13, -20, 15, -45],
          formatter: "涨跌均线" +  warnLine[index],
        }
      })

      if(warnLine.length==index+1){
        map_data_array.push({
          gt: item,
          lte: item>0?maxVal:minVal,
          color: "#ed4014"
        })
      }else{
        map_data_array.push({
          gt: item,
          lte: warnLine[index],
          color: "#26D4AD"
        })
      }
  })
  line_data = [
    {},
    {
      symbol: "none",
      // label: {
      //   position: "middle",
      //   formatter: "警戒线",
      //   color: "red",
      // },
      itemStyle: {
        normal: {
          lineStyle: {
            type: "dashed",
          },
          label: {
            show: true,
            position: "end",
          },
        },
      },
      data: line_data_array,
    },
  ];
  area_data = [
    {},
    {
      min: minVal,
      max: maxVal,
      realtime: true,
      dimension: 1,
      color: {
        type: "linear",
        x: 0,
        y: 1, //此处设置为1，表示从y=0处作为渐变起点
        x2: 0,
        y2: 0,
        colorStops: [
          {
            offset: 0,
            color: "#409eff", // 0% 处的颜色
          },
          ...area_data_item,//全部数据
          {
            offset: 1,
            color: "#ed4014", // 100% 处的颜色
          },
        ],
        global: false, // 缺省为 false
      },
    },
  ];
  map_data = [
    {},
    {
        show: false, // 隐藏视觉映射组件，仅用于驱动颜色
        type: 'piecewise',
        dimension: 1, // 对应数据中的第二个值（体重）
        pieces: [
          {min: minVal, max: warnLine[1], color: '#32CD32'},  // 低于20斤为绿色
          {min: warnLine[1]+1, max: warnLine[0], color: '#1e90ff'},    // 20-60斤为蓝色
          {min: warnLine[0]+1, max:maxVal, color: '#e6a23c'}            // 超过60斤为红色
        ]
    },
  ];
  return {
    line_data,
    area_data,
    map_data
  }
}
export const analyzeTrend=(data,all_time,deal_num)=> {
  let dp_zhi=0,tp_zhi=0;
  if(!deal_num)  deal_num=10000
  if (data.length === 0) return {dp_index: -1, tp_index: -1};
  const lastValue = data[data.length - 1];
  const jishu= Math.trunc(lastValue / deal_num) * deal_num;
  if(jishu>0){
    dp_zhi=jishu-1
    tp_zhi=jishu+deal_num
  }else{
    dp_zhi=jishu-deal_num
    tp_zhi=jishu+1
  }
  let dp_index = -1;
  let tp_index = -1;
  // 寻找最近跌破阈值的位置
  for (let i = data.length - 1; i >= 0; i--) {
      if (data[i] < dp_zhi) {
        dp_index = i;
          break;
      }
  }
  // 寻找最近突破阈值的位置
  for (let i = data.length - 1; i >= 0; i--) {
      if (data[i] > tp_zhi) {
        tp_index = i;
          break;
      }
  }

  return  [
    {num:tp_zhi,index:tp_index,time:all_time[tp_index]},
    {num:dp_zhi,index:dp_index,time:all_time[dp_index]}
  ]
  //'上次突破'+tp_zhi+'时间'+all_time[tp_index]+'上次跌破'+dp_zhi+'时间'+all_time[dp_index];
}
export default {
  initData,
  dealLineAreaMap,
  analyzeTrend

}
